package sk.marek.barak.client;


import java.util.Collections;
import java.util.List;

import sk.marek.barak.shared.ThoughtServiceFactory;
import sk.marek.barak.shared.UserInformationServiceFactory;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.ScrollPanel;

public class UserProfile extends Composite {
	private UserInformation user;
	private TextBox name;
	private DateBox dateBox;
	private AbsolutePanel absolutePanel;
	private FlexTable flexTable_1;
	private static final int REFRESH_INTERVAL = 30000;
	
	private ThoughtServiceAsync thoughtService;
	
	public UserProfile(){
		user = ShareLife.getCurentUser();
		VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		panel.setSize("356px", "386px");
		
		absolutePanel = new AbsolutePanel();
		absolutePanel.setSize("353px", "178px");
		absolutePanel.setStyleName("userProfile");
		panel.add(absolutePanel);
	
		FlexTable flexTable = new FlexTable();
		absolutePanel.add(flexTable, 10, 40);
		flexTable.setSize("200px", "74px");
	
		HTML htmlName = new HTML("Name:");
		flexTable.setWidget(0, 0, htmlName);
	
		name = new TextBox();
		name.setText(user.getName());
		flexTable.setWidget(0, 1, name);
		name.setWidth("130px");
	
	HTML htmlUserID = new HTML("UserID:");
	flexTable.setWidget(1, 0, htmlUserID);
	
	HTML userID = new HTML(user.getUserID());
	flexTable.setWidget(1, 1, userID);
	
	HTML htmlBirthDay = new HTML("Birthdate:");
	flexTable.setWidget(2, 0, htmlBirthDay);
	
	dateBox = new DateBox();
	flexTable.setWidget(2, 1, dateBox);
	dateBox.setValue(user.getBirthDate());
	dateBox.setWidth("130px");

	
	PushButton pshbtnNewButton = new PushButton("x");
	pshbtnNewButton.addClickHandler(new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			removeFromParent();
		}
			
	});
	absolutePanel.add(pshbtnNewButton, 327, 11);
	pshbtnNewButton.setSize("10px", "13px");
	
	PushButton pshbtnSave = new PushButton("Save");
	pshbtnSave.addClickHandler(new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
		saveChanges();
			
		}
	});
	absolutePanel.add(pshbtnSave, 298, 144);
	
	

	
	final ScrollPanel s = new ScrollPanel();
	s.setStyleName("headerPanel");
	s.addScrollHandler(new ScrollHandler() {
		
		@Override
		public void onScroll(ScrollEvent event) {
			
		}
	});
	
	panel.add(s);
	s.setHeight("204px");
	
	flexTable_1 = new FlexTable();
	s.setWidget(flexTable_1);
	flexTable_1.setSize("100%", "100%");
	
	getThoughts();
	Timer timer = new Timer(){

		@Override
		public void run() {
			getThoughts();
			
		}
		
	};
	timer.scheduleRepeating(REFRESH_INTERVAL);

	Window.addResizeHandler(new ResizeHandler() {
		
		@Override
		public void onResize(ResizeEvent event) {
			s.setHeight(String.valueOf(getScrollPanelHeight())+"px");
			
		}
	});
		
	
}
	private void saveChanges(){
		user.setName(name.getText());
		user.setBirthDate(dateBox.getValue());
		UserInfomationServiceAsync userService = UserInformationServiceFactory.getUserInformationService();
		
		
		userService.saveUser(user, new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				Window.alert("Save sucessful");
				
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Save failed");
				
			}
		});
		
	}
	private int getScrollPanelHeight(){
		return  Window.getClientHeight()-(absolutePanel.getAbsoluteTop() +absolutePanel.getOffsetHeight() +50) ;
	}
	
	private void getThoughts(){
		if(thoughtService==null)
			thoughtService = ThoughtServiceFactory.getThoughtService();
			
			thoughtService.getUserThoughts(user.getUserID(), new AsyncCallback<List<UserThought>>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(List<UserThought> result) {
					Collections.sort(result);
					for(int i =0;i<result.size();i++){
						ThoughtUI thoughtUI=new ThoughtUI(result.get(i));
						flexTable_1.setWidget(i, 0, thoughtUI);
					}
					if(flexTable_1.getRowCount()>result.size()){
						for(int i = result.size();i<flexTable_1.getRowCount();i++)
							flexTable_1.removeRow(i);
					}
				
					
				}
			});
		
	}
}
